package cn.qdhlm.collectservice.core.component.analyse;


import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.util.CodeUtil;

import cn.qdhlm.collectservice.service.UtilityService;
import io.netty.buffer.ByteBuf;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Name: AnyAfn02
 * @Description: 链路接口检测（AFN=02H）
 * @Author: jiangcs
 */
@Log4j2
@Component
public class AnyAfn02 {
    @Autowired
    private UtilityService utilityService;

    public boolean execute(int da, int dt, String collectorNo, ByteBuf byteBuf) throws Exception {
        // 读取版本号： 7个字节
        StringBuilder sbVersion = new StringBuilder();
        for (int i = 0; i < Constant.BYTE_LENGTH_VERSION; i++) {
            sbVersion.append(CodeUtil.hexStringToAsciiString(CodeUtil.byteToHexString(byteBuf.readByte())));
        }

        int type = -1;
        // F1：登录 0x0100
        if (da == Constant.DA_0 && dt == Constant.DT_256) {
            type = Constant.COLLECTOR_ONLINE_TYPE_REG;
        }
        // F2：心跳 0x0200
        else if (da == Constant.DA_0 && dt == Constant.DT_512) {
            type = Constant.COLLECTOR_ONLINE_TYPE_HEART;
        }

        if (type != -1) {
            // 数据存储至mongodb
            utilityService.sendOnlineData(type, collectorNo, sbVersion.toString(), null, null);

            log.info("execute: da[{}], dt[{}], type[{}]", da, dt, type);
            return true;
        }

        log.error("execute: da[{}], dt[{}] match fail", da, dt);
        return false;
    }
}
